symbols <- c("AA", "JPM")

# for visual chart output, including signals 
TestFrame <- function(symbols, n1=14, n2=32, n3=64, TimesSD=2, subset="2009/2013") {
  for (sym in symbols) {
    #sym.ATRD <- ATRDiff(get(sym), n1=n1, n2=n2)
    #sym.vT <- volaThreshold_SD(sym.ATRD, n=n3, TimesSD=TimesSD)
    #plot(sym.ATRD)
    #lines(sym.vT, col="green")
    sym.vaT <- volaAboveThreshold_SD(get(sym), n1=n1, n2=n2, n3=n3, TimesSD=TimesSD)
    sym.slopes <<- detectSlopes(sym.vaT)
    chartSeries(get(sym), TA= 'addTA(sym.slopes[,"falling"], on=1)', name=sym ,subset=subset)
  }
}

writeVolaDb <- function(symbols, n1=14, n2=32, n3=64, TimesSD=2, subset="2001/2012") {
  columns <- c('symbol', 'date', 'ATRs', 'ATRl', 'ATRDiff', 'vThreshold', 'sig_VgtTh', 'sig_VcdTh',
               '5d_b_s', '5d_s_s', '5d_ret', '5d_volaPct',
               '10d_b_s', '10d_s_s', '10d_ret', '10d_volaPct',
               '20d_b_s', '20d_s_s', '20d_ret', '20d_volaPct',
               '40d_b_s', '40d_s_s', '40d_ret', '40d_volaPct')
  futurePeriods <- c(5, 10, 20, 40)
  tblname <- paste("RES_VOLA", n1, n2, n3, TimesSD, sep="_")
  for (sym in symbols) {
    sym.ATRD <- ATRDiff(get(sym), n1=n1, n2=n2)
    sym.vT <- volaThreshold_SD(sym.ATRD, n=n3, TimesSD=TimesSD)
    sym.vaT <- sym.vT > 0 & sym.ATRD[,"ATRDiff"] > sym.vT
    colnames(sym.vaT) <- c("sig_VgtTh")
    sym.slopes <- detectSlopes(sym.vaT)
    colnames(sym.slopes) <-c("sig_VcuTh", "sig_VcdTh")
    dbxts <- cbind(sym.ATRD, sym.vT, sym.vaT, sym.slopes[,"sig_VcdTh"])
    for (per in futurePeriods) {
      sym.BSs <- findOpportunity(get(sym), n=per, method="o1")
      
    }

  }
}

ATRDiff <- function(TS, n1=14, n2=32) {
  ATR1 <- ATR(cbind(Hi(TS), Lo(TS), Cl(TS)), n1, EMA)
  ATR2 <- ATR(cbind(Hi(TS), Lo(TS), Cl(TS)), n2, EMA)
  result <- cbind(ATR1[,"atr"]-ATR2[,"atr"], ATR1[,"atr"], ATR2[,"atr"])
  colnames (result) <- c("ATRDiff", "ATRs", "ATRl")
  return (result)
}

# seems to work better than the quantile-threshold
volaThreshold_SD <- function(TS.ATRDiff, n=64, TimesSD=2) {
  TS.Mean <- runMean(TS.ATRDiff, n=n)
  TS.SD <- runSD(TS.ATRDiff, n=n)
  TS.Threshold = TS.Mean[,1] + TS.SD[,1]*TimesSD
  colnames (TS.Threshold) <- "vThreshold"
  return (TS.Threshold)
}

# seems to work better than the quantile-threshold 
volaAboveThreshold_SD <- function(TS, n1=14, n2=32, n3=64, TimesSD=2) {
  TS.AD <- ATRDiff(TS, n1, n2)
  TS.vT <- volaThreshold_SD(TS.AD, n=n3, TimesSD=TimesSD)
  result <- TS.vT > 0 & TS.AD[,"ATRDiff"] > TS.vT
  colnames (result) <- "sig_VgtTh"
  return(result)
}

# not recommended
volaThreshold_QT<- function(TS.ATRDiff, n=64, probs=.95) {
  TS.Threshold = rollapply(TS.ATRDiff, n, quantile, probs=probs, na.rm=TRUE)
  colnames (TS.Threshold) <- "Threshold"
  return (TS.Threshold)
}

# not recommended
volaAboveThreshold_QT <- function(TS, n1=14, n2=32, n3=64, probs=.95) {
  TS.AD <- ATRDiff(TS, n1, n2)
  TS.vT <- volaThreshold_QT (TS.AD, n3, probs)
  result <- TS.vT > 0 & TS.AD > TS.vT
  colnames (result) <- "VolaSpike"
  return(result)
}

volaAboveThresholdHysteresis <- function(TS, n1=14, n2=32, n3=64, prob1=.95, prob2=.9) {
  TS.AD <- ATRDiff(TS, n1, n2)
  TS.vTHi <- volaThreshold(TS.AD, n3, prob1)
  TS.vTLo <- volaThreshold(TS.AD, n3, prob2)
  vgtHiTh <- TS.AD > TS.vTHi
  vgtLoTh <- TS.AD > TS.vTLo
  result <- cbind(vgtHiTh, vgtLoTh)
  colnames (result) <- c("vgtHiTh", "vgtLoTh")
  return(result)
}

detectSlopes <- function(TS) {
  LAGTS <- Lag(TS[,1],1)
  falling <- LAGTS > TS
  rising <- LAGTS < TS
  result <- cbind (rising, falling)
  colnames(result) <- c("rising", "falling")
  return (result)
}

detectSlopesHysteresis <- function(TS2Col) {
  TS3Col<-cbind(TS2Col, FALSE)
  posOpen <- FALSE
  for (i in 1:nrow(tmp)) {
    if (is.na(TS3Col[i,1]) | is.na(TS3Col[i,2])) {
      TS3Col[i,3] <- NA
    } else if (TS3Col[i,1] & TS3Col[i,2] | posOpen & TS3Col[i,2]) {
      TS3Col[i,3] <- TRUE
      posOpen <- TRUE
    } else {
      TS3Col[i,3] <- FALSE
      posOpen <- FALSE
    }
  }
  LAGTS <- Lag(TS3Col[,3],1)
  falling <- LAGTS > TS3Col[,3]
  rising <- LAGTS < TS3Col[,3]
  result <- cbind (rising, falling)
  colnames(result) <- c("rising", "falling")
  return (result)
}

symbols <- c("AA", "ABX", "AEP", "ADM", "CSCO", "F", "IBM", "INTC", "JPM", "KO", "MGM", "MMM", "MO", "MSFT", "SSRI", "X", "XOM", "XRX")

retrieveTestGroup <- function(symbols, from="2000-01-01") {
  for (sym in symbols) {
    getSymbols(sym, env=globalenv(), from=from, adjust=TRUE)
  }
}